<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("moduleFunction.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= subModMod();
	$module = sxREQUEST("module");
	$subModule = sxREQUEST("sub_module");
	$newSubModule = sxREQUEST("new_sub_module");
	$functionalArea = sxREQUEST("functional_area");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$doc_name = sxREQUEST("doc_name");
}elseif(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "delete"){
	$errMsg .= subModDel();
	$module = sxREQUEST("module");
	$subModule = sxREQUEST("sub_module");
	$newSubModule = sxREQUEST("new_sub_module");
	$functionalArea = sxREQUEST("functional_area");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$doc_name = sxREQUEST("doc_name");
}else{
	$module = sxREQ('mod');
	$subModule = $newSubModule = sxREQ('sub');
	if($module==''){
		$url = $_SESSION[USEROBJ]->getHistory(0, true).
			"&msg=".urlencode(_t("var 'mod' missing")); 
		serverRedir($url);
	}
	if($subModule==''){
		$url = $_SESSION[USEROBJ]->getHistory(0, true).
			"&msg=".urlencode(_t("var 'sub' missing")); 
		serverRedir($url);
	}
	$sSQL = "SELECT * FROM sub_module " .
				"WHERE module = '".$module."' AND sub_module = '".$subModule."'";
	$objSDAO->query($sSQL, "mod");
	if(!$row = $objSDAO->fetch("mod")){
		$url = $_SESSION[USEROBJ]->getHistory(0, true).
			"&msg=".urlencode(_t("sottomodulo '%s' inesistente",$module.".".$subModule)); 
		serverRedir($url);
	}
	$functionalArea = $row["functional_area"];
	$description = $row["description"];
	$note = $row["note"];
	$doc_name = '';
}

//output HTML
$aOpz = array("kMenu"=>'pro.mod', "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			var objDA =  document.getElementById("sub_module");
			var objDe =  document.getElementById("description");
			var objMessArea =  document.getElementById("messArea");
			var errMsg = "";
			if(objDe.value == ""){
				errMsg += "<?php echo _t("description missing.")?><br>";
				objDe.focus();
			}
			if(objDA.value == ""){
				errMsg += "<?php echo _t("name missing.")?><br>";
				objDA.focus();
			}
			if(errMsg == ""){
				objFrm.submit();
			}else{
				objMessArea.innerHTML = errMsg;
			}
		}catch(e){
			raiseError(e.description, "save update sub-module");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"subModMod.php\" method=\"POST\" enctype=\"multipart/form-data\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">\n");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">\n");
echo("<input type=\"hidden\" name=\"module\" id=\"module\" value=\"$module\">\n");
echo("<input class=\"inpt01\" type=\"hidden\" name=\"sub_module\" id=\"sub_module\" value=\"$subModule\">");
echo("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n<tr>\n<td>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='10%'>Module</td>" .
			"<td class=\"text01\" width='90%'>$module</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Functional-Area</td>" .
			"<td class=\"text01\">\n<select name='functional_area'>\n");
$sSQL = "SELECT * FROM functional_area WHERE module = '$module'";
$objSDAO->query($sSQL,'fa');
while($rowFA=$objSDAO->fetch('fa')) {
	echo("<option value=\"".$rowFA['functional_area']."\" ".
		(($rowFA['functional_area']==$functionalArea)?'SELECTED':'').">"
		.$rowFA['functional_area']." - ".$rowFA['description']."</option>\n");
}
echo(		"</td>" .
		"</tr>\n</table>");

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati del nuovo sotto-modulo"),
										"text"=>_t("conferma")
										),
					"back"=>array("pag"=>$objUser->getHistory(0, false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Sotto modulo")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Name")." (*)</td>" .
			"<td class=\"text01\">" .
			"<input class=\"inpt01\" type=\"text\" name=\"new_sub_module\" id=\"new_sub_module\" value=\"$newSubModule\" size='4' maxlength='4'></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")." (*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
?>
